let express=require("express")();
let mysql=require("mysql");

const port=8080;
express.all("/*", function(req, res, next) {
    // 跨域处理
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1');
    res.header("Content-Type", "application/json;charset=utf-8");
    next(); // 执行下一个路由
})
let sql=mysql.createConnection({
    host:"localhost",
    user:"root",
    password:"123456",
    database:"student",
    timezone:"08:00"

})
sql.connect();
// 登录
express.get("/submit",(request,response)=>{
    let username=request.query.username;
    let password=request.query.password;
    console.log(username);
    console.log(password);
    sql.query(`SELECT * FROM student5 WHERE username="${username}" AND password="${password}"`,(error,data)=>{
      
        if(error){
            console.log(2);
            
            response.send("error");
                    
        }
        else{
            console.log(1);
            if(!data.length){
                response.send("error")
            }
            else{
                response.send("success")
            }
        }
    })
})
// 注册
express.get("/adduser",(request,response)=>{
    let username=request.query.username;
    let password=request.query.password;
    sql.query(`INSERT INTO student5 (username,password) VALUES ("${username}","${password}")`,(error)=>{
  
        if(error){
            console.log(error)
            response.send("error");
                    
        }
        else{
          
                response.send("success")
            
        }
    })
})
// // 获取数据
// express.get("/getinfrom",(request,response)=>{
// const id=request.query.id;
//     let s=id?`SELECT * FROM student4 WHERE id="${id}"`: `SELECT * FROM student4 ORDER BY id`;
//     sql.query(s,(error,data)=>{
//         if(error){
//             console.log(data);
//             response.send("error")
//         }
//         else{
//             response.send(data)
//         }
//     })
    
// })
// // 删除数据
// express.get("/removeinfrom",(request,response)=>{
//     const id=request.query.id;
//    console.log(id)
//         sql.query(`DELETE  FROM student4 WHERE id="${id}"`,(error,data)=>{
//             if(error){
//                 console.log(data);
//                 response.send("error")
//             }
//             else{
//                 response.send("success")
//             }
//         })
        
//     })
//     // 添加数据
//     express.get("/addStudent",(request,response)=>{
//         const name = request.query.name;
//         const age = request.query.age;
//         const sex = request.query.sex;
//         const city = request.query.city;
//         const joinDate = request.query.joinDate;
//         if(name && age && sex && city && joinDate){
//             sql.query(`INSERT INTO student4 (name,age,sex,city,joinDate) VALUES ("${name}","${age}","${sex}","${city}","${joinDate}")`,(error,data)=>{
//                 if(error){
//                     console.log(error)
//                     response.end("error")
//                 }
//                 else{
//                     response.send("success")
//                 }
//             })
//         }
//         else{
//             response.end("error");
//         }
//     })
express.listen(port);
console.log("server is running at " + port)
